package activity;



import com.qzhy.female.R;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import dilog.MyDialog;
import tool.AppManager;
import widget.BaseDialog;

public class BaseActivity extends FragmentActivity {

	MyDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getAppManager().addActivity(this);

		// loading.setCancelable(false);
	}

	private void getDialogInstance() {

		if (dialog == null) {
			dialog = new MyDialog(this);
			dialog.setCanceledOnTouchOutside(true);
		}
	}

	
	public void showLoadingDialog() {
		getDialogInstance();
		dialog.setCancelable(true);
		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {

				BaseActivity.this.finish();

			}
		});
		dialog.show();
	}
	
	
//	/**
//	 * 绑定返回事件
//	 */
//	protected void bindBackButton() {
//		findViewById(R.id.btn_back).setOnClickListener(
//				new View.OnClickListener() {
//
//					@Override
//					public void onClick(View v) {
//						finish();
//				}
//		});
//
//	}
//	/**
//	 * 
//	 */
//	protected void showLoadingDialogCancleable(){
//		getDialogInstance();
//		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
//
//			@Override
//			public void onCancel(DialogInterface dialog) {
//				BaseActivity.this.finish();
//			}
//		});
//		dialog.show();
//	}

	public void updateDialogDes(String str) {
		if (null != dialog) {
			if (dialog.isShowing()) {
				dialog.setText(str);
			}
		}
	}

	protected void showLoadingDialog(String text) {
		getDialogInstance();
		if (text != null) {
			dialog.setText(text);
		}
//--------------update
		dialog.setCanceledOnTouchOutside(true);

		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {

				BaseActivity.this.finish();

			}
		});
		dialog.show();
	}

	/**
	 * 禁止关闭的dialog
	 * @param text
	 */
	protected void showLoadingDialogCannotCancle(String text) {
		getDialogInstance();
		if (!TextUtils.isEmpty(text)) {
			dialog.setText(text);
		}
		dialog.setCanceledOnTouchOutside(false);
		dialog.setCancelable(false);
		dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					Toast.makeText(getApplicationContext(), "此阶段不能返回", Toast.LENGTH_SHORT).show();
					return false;
//					return ture;
				}
				return false;
			}
		});
		dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {

				BaseActivity.this.finish();

			}
		});
		dialog.show();
	}
	
	public void showToast(String text){
		Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
	}
	private static Toast toast;
	public  static void showToast(Context context, String text) {
//		Toast toast = Toast.makeText(this, "toast", Toast.LENGTH_LONG);
		// ���֮ǰ�����񣬾�ȡ��
		if (toast != null){
			toast.cancel();
		}
		toast = new Toast(context);
		View view = LayoutInflater.from(context).inflate(R.layout.custom_toast, null);
		TextView tvText = (TextView) view.findViewById(R.id.tv_load);
		tvText.setText(text);
		// toast.setBackground();
		toast.setView(view);
		toast.setGravity(Gravity.TOP, 0, 60);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.show();
	}
	
	public void showDialog(String msg){
		BaseDialog mBackDialog = BaseDialog.getDialog(this, "提示",
				msg, "确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel(); 
					}
				});
		mBackDialog.setCancelable(false);
		mBackDialog.show();
	}

	public void dismissLoadingDialog() {
		if (dialog!=null&&dialog.isShowing()) {
			dialog.dismiss();
		}
	}
	/**
	 * 网络错误处理
	 * @param statusCode
	 * @param error
	 */
	public void networkError(int statusCode,Throwable error){
		Toast.makeText(getApplicationContext(), "网络错误:"+error.getMessage()+statusCode, Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * 登录长时间未操作
	 */
	public void loginTimeOut(){
		
	}
	@Override
	protected void onDestroy() {
		// 结束Activity&从堆栈中移除
		AppManager.getAppManager().finishActivity(this);
		super.onDestroy();
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return null;
	}
}
